<?php
namespace app\home\controller;

use \app\home\model\Paper;
use \think\Session;
use \think\Db;
use \think\Validate as Validate;
class Examination extends Home{

    /**
     * 开始进入考试页面。做好答题准备！
     * 
     * @return html
     * @date Thu Jun 21 09:26:55 2018
     */
    public function index(){
        
    }

    /**
     * 试题页面
     * 
     * @param int $paper_id
     * @return html
     * @date Thu Jun 21 17:30:33 2018
     */
    public function begin(){
        $paper_id = $this->req->param('id');
        $paper_model = new \app\home\model\Paper;
        $paper = $paper_model->where(['pap_id'=>$paper_id])->find();
        $choice_model = new \app\home\model\Choice;
        $question = $choice_model->where(['pap_id'=>$paper_id])->select();
        foreach($question as $key=>$value){
            $value['ch_options'] = unserialize($value['ch_options']);
            // var_dump($value['ch_options']);
        }
        
        $this->assign('paper',$paper);
        $this->assign('data',$question);
        return $this->fetch();
    }

    /**
     * 回答问题提交
     * 
     * @return json
     * @date Thu Jun 21 17:32:35 2018
     */
    public function answer(){
        $data = $this->req->param();
        $rule = [
            'papid'  => 'require'
        ];

        $varidate = new Validate;
        $bool     = $varidate->check($rule);
        
        if($bool && empty($data['answer'])){
            echo $this->alert(1,'Wrong');
        }else{
            $answer = [];
            $papid  = $data['papid'];
            foreach($data['answer'] as $value){
                $temp               = explode('-',$value);
                $answer[$temp[0]][] = $temp[1];

            }
            $model  = new \app\home\model\Choice;
            $choice = $model->where(['pap_id'=>$papid])->select();
            $result = [];
            foreach($choice as $key=>$value){
                if(!isset($answer[$value['ch_id']])){
                    $answer[$value['ch_id']] = [null];
                }
                $result[$value['ch_id']] = unserialize($value['ch_result']);
            }
            $stu_result = [];
            $right = 0;
            $total = count($result);
            foreach($result as $key=>$res){
                if($res == $answer[$key]){
                    $stu_result[$key] = true;
                    $right += $right;
                }else{
                    $stu_result[$key] = false;
                }
            }

            $info = [
                'ans_stuid'      => Session::get('stu.id'),
                'ans_value'      => serialize($answer),
                'ans_papid'      => $papid,
                'ans_result'     => serialize($stu_result),
                'ans_createtime' => time(),
                'ans_accurary' => $right . '/'.$total,
            ];
            $answer_model = new \app\home\model\Answer;
            $bool         = $answer_model->save($info);
            // $bool = 1;
            if($bool){
                $last = [
                    'answer' => $result,
                    'result' => $stu_result
                ];
                echo $this->alert(1,'Show the answers',$last);
            } else {
                echo $this->alert(0,'Systems Wrong');
            }
        }
    }

}
